import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { LoginInfoService } from './login-info.service';

@Injectable({
  providedIn: 'root'
})
export class HttpService {

  constructor(private httpClient: HttpClient,
    private _loginInfoService: LoginInfoService) { }

  get<T>(url: string): Observable<T> {
    const headers = this._loginInfoService.headers();
    const response = this.httpClient.get<T>(url, { headers: headers });

    return response;
  }

  getWithHeaders<T>(url: string, headers: HttpHeaders): Observable<T> {
    return this.httpClient.get<T>(url, {headers: headers});
  }

  getWithParam<T>(url: string, param: any): Observable<T> {
    const token = this._loginInfoService.getToken();

    for (const field in param) {
      console.log(`check ${field} value ${param[field]}`);
      if (param[field] == null || param[field] == undefined) {
        console.log(`delete param ${field} with ${param[field]}`)
        delete param[field];
      }
    }
    const httpParam = new HttpParams({fromObject: param});

    console.log(`start to get with param: ${url} : ${token}`);
    const httpHeaders = new HttpHeaders().set('Authorization', 'Bearer ' + token)
    .set('Content-Type', 'text/plain');
    const response = this.httpClient.get<T>(url, {headers: httpHeaders, params: httpParam});
//    let response = this.httpClient.get<T>(url, {headers: headers, params: httpParam});

return response
  }

  postWithoutToken<T>(url: string, param: any): Observable<T> {
    const headers = new HttpHeaders();
    const response = this.httpClient.post<T>(url, param, {headers: headers});

    return response;
  }

  post<T>(url: string, param: any): Observable<T> {
    const headers = this._loginInfoService.headers();

    const response = this.httpClient.post<T>(url, param, {headers: headers});

    return response;
  }

  postWithHeaders<T>(url: string, param: any, headers: HttpHeaders): Observable<T> {
    return this.httpClient.post<T>(url, param, {headers: headers});
  }

  deleteByIds<T>(url: string, ids: number[]): Observable<T> {
    const headers = this._loginInfoService.headers();

    const paramIds = ids.join(",");
    url = url + paramIds;
    const response = this.httpClient.delete<T>(url, { headers: headers});

    return response;
  }

  delete<T>(url: string): Observable<T> {
    const headers = this._loginInfoService.headers();

    return this.httpClient.delete<T>(url, { headers: headers});
  }

  updateEntity<T>(url: string, entity: any): Observable<T> {
    const headers = this._loginInfoService.headers();

    const response = this.httpClient.put<T>(url, entity, { headers: headers});

    return response;
  }

  putWithBody<T>(url: string, entity: any): Observable<T> {
    const headers = this._loginInfoService.headers();

    const response = this.httpClient.put<T>(url, entity, { headers: headers});

    return response;
  }

  put<T>(url: string): Observable<T> {
    const headers = this._loginInfoService.headers();

    const response = this.httpClient.put<T>(url, {}, { headers: headers});

    return response;
  }
}
